package dao.impl;

import entity.TeacherEntity;
import dao.TeacherDao;
import util.JDBCUtil;

import java.util.List;
import java.util.Map;

public class TeacherDaoImpl extends JDBCUtil implements TeacherDao {

    @Override
    public List<Map<String, Object>> getTeacherInfo(String name) {
        String sql;
        List<Map<String, Object>> mapList;
        if ("null".equalsIgnoreCase(name)) {
            sql = "select * from teacher_info";
            mapList = queryForList(sql);
        } else if (name != null && !name.trim().isEmpty()) {
            sql = "select * from teacher_info where teacher_name like ?";
            String teacherName = "%" + name + "%";
            mapList = queryForList(sql, teacherName);
        } else {
            sql = "select * from teacher_info";
            mapList = queryForList(sql);
        }
        return mapList;
    }

    @Override
    public int addTeacherInfo(TeacherEntity teacher) {
        String sql = "insert into teacher_info (id,academy_id,teacher_name,age,gender,phone_number,degree) values (?,?,?,?,?,?,?)";
        return insert(sql, teacher.getId(), teacher.getAcademyId(), teacher.getTeacherName(), teacher.getAge(), teacher.getGender(), teacher.getPhoneNumber(), teacher.getDegree());
    }

    @Override
    public int updateTeacherInfo(TeacherEntity teacher) {
        String sql = "update teacher_info set academy_id = ?,teacher_name = ?,age = ?,gender = ?,phone_number = ?,degree = ? where id = ?";
        return update(sql, teacher.getAcademyId(), teacher.getTeacherName(), teacher.getAge(), teacher.getGender(), teacher.getPhoneNumber(), teacher.getDegree(), teacher.getId());
    }

    @Override
    public int deleteTeacher(String teacherId) {
        String sql = "delete teacher_info from teacher_info where id = ?";
        return delete(sql, teacherId);
    }

    @Override
    public int batchDeleteTeacher(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        StringBuilder placeholders = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            placeholders.append("?");
            if (i < ids.size() - 1) {
                placeholders.append(",");
            }
        }

        String sql = "delete from teacher_info where id in (" + placeholders + ")";
        return delete(sql, ids.toArray());
    }

    @Override
    public List<Map<String, Object>> getTeacherInfoByAcademyId(String academyId) {
        String sql = "select * from teacher_info where academy_id = ?";
        return queryForList(sql,academyId);
    }

    @Override
    public Map<String, Object> getTeacherInfoById(String id) {
        String sql = "select * from teacher_info where id = ?";
        return queryForList(sql,id).get(0);
    }
}
